package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.db.dto.UserDTO;
import com.example.communist_app_api.db.pojoDto.User;
import com.example.communist_app_api.service.RoleService;
import com.example.communist_app_api.service.UserRoleService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.CosUtils;
import com.example.communist_app_api.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;


/**
 * 用户controller控制器
 *
 * @author love~mlm
 * @version 1.0
 * Create by 2022/12/5 17:35
 */
@RestController
@RequestMapping("/sys/user")
@Api("用户controller控制器")
public class SysUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /*
    修改或添加个人信息
     */
    @PostMapping("/save")
    @ApiOperation("修改或添加个人信息")
    public R save(@RequestBody User user) {
        if (user.getId() == null || user.getId() == -1) {

        } else {
            user.setUpdatedTime(new Date());
            userService.editPersonMessage(user);
        }
        return R.ok();
    }

    /*
    修改密码
     */
    @PostMapping("/updateUserPwd")
    @ApiOperation("修改密码")
    public R updateUserPwd(@RequestBody UserDTO userDTO) {
        User user = userService.getUserByUserId(userDTO.getId());
        //TODO由于刚开始加密不成功，有问题，所以这里暂时注释掉正确的代码，先用明文代替判断先，代码逻辑一样
        /*
        //encodes不能用equals判断，只能用match
        if (bCryptPasswordEncoder.matches(userDTO.getOldPassword(), user.getPassword())){
           user.setPassword(bCryptPasswordEncoder.encode(userDTO.getNewPassword()));
           user.setUpdatedTime(new Date().getTime());
           userService.editPersonMessage(user);
       }else {
           return R.error("输入旧密码错误！");
       }
         */

        //暂时性代码开始
        if (user.getPassword().equals(userDTO.getOldPassword())) {
            user.setPassword(userDTO.getNewPassword());
            user.setUpdatedTime(new Date());
            userService.editPersonMessage(user);
        } else {
            //否则报错，旧密码不对
            return R.error("输入旧密码错误！");
        }
        //暂时性代码结束

        return R.ok();
    }

    @PostMapping("/uploadImage")
    @ApiOperation("上传头像")
    public R uploadImage(MultipartFile multipartFile) throws Exception {
        if (multipartFile == null) {
            return R.error("上传失败");
        }
        System.out.println("multipartFile = " + multipartFile);
        //MultipartFile multipartFile = BASE64DecodedMultipartFile.base64ToMultipart(base64);
        String photoUrl = CosUtils.uploadFile(multipartFile, "");
        return R.ok("上传成功").put("imageUrl", photoUrl);
    }

    /*
          修改或添加个人信息
      */
    @PostMapping("/updateAvatar")
    @ApiOperation("修改头像")
    public R updateAvatar(@RequestBody UserDTO userDTO) {
        User user = userService.getUserByUserId(userDTO.getId());
        user.setPhoto(userDTO.getPhoto());
        user.setUpdatedTime(new Date());
        userService.editPersonMessage(user);
        return R.ok();
    }

}
